package com.macro.mall.controller.commission;

import com.github.pagehelper.Page;
import com.macro.mall.commission.domain.OrderPredicate;
import com.macro.mall.commission.entity.OrderPredicateEntity;
import com.macro.mall.commission.service.OrderPredicateService;
import com.macro.mall.commission.service.bo.PagedQueryOrderPredicateInput;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.input.PageConstants;
import com.macro.mall.dto.commission.PagedQueryPredictReqDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * class
 *
 * @author xuyanjun
 * @date 2020-01-22
 */
@Api(tags = "收益管理")
@Slf4j(topic = "PredictController")
@RestController
@RequestMapping("/predict")
public class PredictController {

    @Autowired
    private OrderPredicateService orderPredicateService;

    /**
     * 分页查询收益
     *
     * @return 分页后的会员
     */
    @ApiOperation("分页查询收益")
    @PostMapping("pagedQuery")
    public CommonPage<OrderPredicate> pagedQuery(@RequestBody PagedQueryPredictReqDto reqDto) {
        PagedQueryOrderPredicateInput pagedQueryOrderPredicateInput = new PagedQueryOrderPredicateInput();
        pagedQueryOrderPredicateInput
                .setUserId(reqDto.getUserId())
                .setCanceled(false)
                .setSortField(PagedQueryOrderPredicateInput.CREATE_TIME_FIELD_NAME)
                .setSortDirection(PageConstants.DESC_SORT_DIRECTION)
                .setPageIndex(reqDto.getPageIndex())
                .setPageSize(reqDto.getPageSize());

        Page<OrderPredicateEntity> page = orderPredicateService.pagedQuery(pagedQueryOrderPredicateInput);
        List<OrderPredicate> orderPredicates = orderPredicateService.entityToDomain(page);

        return CommonPage.restPage(orderPredicates, page);
    }
}
